package com.baseweb.webadmin.core.util;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CitiesUtil {

    public static Map<String, String> CITIES_MAP = getCities();

    public static Map<String, String> CITIES_CODE_MAP = getCityCodes();

    public static List<String> SPECIAL_CITIES = Arrays.asList("上海市","重庆市","天津市","北京市","香港特别行政区","澳门特别行政区");

    public static Map<String, String> getCities(){
        Map<String, String> result = new HashMap<String, String>();
        InputStream in = ConfigUtil.class.getResourceAsStream("/config/cities.json");

        ObjectMapper mapper = new ObjectMapper();
        TypeReference<HashMap<String,Object>> typeRef
                = new TypeReference<HashMap<String,Object>>() {};

        try {
            result = mapper.readValue(in, typeRef);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static Map<String, String> getCityCodes(){
        Map<String, String> result = new HashMap<String, String>();

        for(Iterator it = CITIES_MAP.keySet().iterator(); it.hasNext();){
            String mKey = it.next().toString();
            result.put(CITIES_MAP.get(mKey), mKey);
        }

        return result;
    }

    public static List<String> getProvincesList(){
        List<String> provinces = new ArrayList<String>();
        for(Iterator it = CITIES_MAP.keySet().iterator(); it.hasNext();){
            String mKey = it.next().toString();

            if (mKey.substring(2,6).equalsIgnoreCase("0000")){
                provinces.add(CITIES_MAP.get(mKey));
            }
        }

        return provinces;
    }

    //依据省份获取省份下所有市
    public static List<String> getCitiesListByProvinceName(String provinceName){
        String provinceCode = provinceName;
        if (!isNumeric(provinceName)) {
            provinceCode = CITIES_CODE_MAP.get(provinceName);
        }

        List<String> cities = new ArrayList<String>();
        for(Iterator it = CITIES_MAP.keySet().iterator(); it.hasNext();){
            String mKey = it.next().toString();

            if (mKey.substring(0,2).equalsIgnoreCase(provinceCode.substring(0,2)) && mKey.substring(4,6).equalsIgnoreCase("00")){
                cities.add(CITIES_MAP.get(mKey));
            }
        }
        return cities;
    }

    //依据城市名获取城市下的区
    public static List<String> getAreasListByCityName(String cityName){
        String cityCode = cityName;
        if (!isNumeric(cityName)) {
            cityCode = CITIES_CODE_MAP.get(cityName);
        }

        List<String> areas = new ArrayList<String>();
        for(Iterator it = CITIES_MAP.keySet().iterator(); it.hasNext();){
            String mKey = it.next().toString();

            if (SPECIAL_CITIES.contains(cityName)){
                int special = Integer.valueOf(cityCode!=null ?cityCode.substring(0, 4):null)+1;
                if (mKey.substring(0, 4).equalsIgnoreCase(""+special)) {
                    areas.add(CITIES_MAP.get(mKey));
                }
            }else {
                if (mKey.substring(0, 4).equalsIgnoreCase(cityCode!=null ?cityCode.substring(0, 4):null) && !mKey.substring(4,6).equalsIgnoreCase("00")) {
                    areas.add(CITIES_MAP.get(mKey));
                }
            }
        }
        return areas;
    }

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
//        System.out.println(getCities());
//        System.out.println(getCitiesListByProvinceName("重庆市"));
//        System.out.println(getAreasListByCityName("重庆市"));

        System.out.print(new BigDecimal(0.00).equals(BigDecimal.ZERO));
    }
}
